
import { request } from "../../request/index"
import regeneratorRuntime from '../../lib/runtime/runtime';
 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    leftMeuaList:[],
    rightCotent:'',
    currentIndex:0,
    scrollTop:0
  },
  Cates:[],
   
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取分类数据 
    let Cates = wx.getStorageSync('Cates')
    if(!Cates){
      this.getCates()
    }else{
      // 设置过期时间 为 10s
      if( Date.now() - Cates.ctime  > 1000 *10){
        this.getCates()
      }else{
         this.Cates = Cates.data
          // 左侧的数据
          let leftMeuaList = this.Cates.map(v=>v.cat_name)
          let rightCotent = this.Cates[0].children 
          // 右侧的数据 
          this.setData({
            leftMeuaList,
            rightCotent, 
          })
      }
    }
  },
  // 分类数据
   async getCates(){  
      const res = await request({url:"/categories"}) 
      this.Cates = res  
        wx.setStorageSync('Cates', {
          data:this.Cates,
          ctime: Date.now()
        })
        // 左侧的数据
        let leftMeuaList = this.Cates.map(v=>v.cat_name)
        let rightCotent = this.Cates[0].children 
        // 右侧的数据 
        this.setData({
          leftMeuaList,
          rightCotent
        })

   },
  // 点击事件
  handClickCate(e){ 
    let {index} = e.currentTarget.dataset 
    this.setData({
      currentIndex:index,
      rightCotent: this.Cates[index].children,
      scrollTop:0
    })  
 } 
})